﻿using Microsoft.SharePoint;
using Microsoft.SharePoint.Publishing;
using System;
using System.Text;
using System.Web.UI;
using System.Web.UI.WebControls;
using VPLink2013.Framework.Constants;

namespace VPLink2014
{
    public partial class OffresInternes : PublishingLayoutPage
    {
        protected Button btnPostuler;

        private void SetInformationSection(Panel panelSection, Label labelSection, string infoSection)
        {
            labelSection.Text = infoSection;
            panelSection.Visible = true;
        }

        private void SetInformationsForm(string commentaires, string direction, string departement, string service, string envTravail, string
            metier, string titre, string tpsTravail, string mission, string profil, string lieu, string reference)
        {
            if (!string.IsNullOrEmpty(commentaires))
            {
                SetInformationSection(pnlCommentaire, lblCommentaire, commentaires);
            }
            if (!string.IsNullOrEmpty(direction))
            {
                SetInformationSection(pnlDirection, lblDirection, direction);
            }
            if (!string.IsNullOrEmpty(departement))
            {
                SetInformationSection(pnlDepartement, lblDepartement, departement);
            }
            if (!string.IsNullOrEmpty(service))
            {
                SetInformationSection(pnlService, lblService, service);
            }
            if (!string.IsNullOrEmpty(envTravail))
            {
                SetInformationSection(pnlEvnTravail, lblEvnTravail, envTravail);
            }
            if (!string.IsNullOrEmpty(metier))
            {
                SetInformationSection(pnlMetier, lblMetier, metier);
            }
            if (!string.IsNullOrEmpty(titre))
            {
                SetInformationSection(pnlIntitulePoste, lblIntutilePoste, titre);
            }
            if (!string.IsNullOrEmpty(tpsTravail))
            {
                SetInformationSection(pnlTpsTravail, lblTpsTravail, tpsTravail);
            }
            if (!string.IsNullOrEmpty(mission))
            {
                SetInformationSection(pnlDscMission, lblDscMission, mission);
            }
            if (!string.IsNullOrEmpty(profil))
            {
                SetInformationSection(pnlProfil, lblProfil, profil);
            }
            if (!string.IsNullOrEmpty(lieu))
            {
                SetInformationSection(pnlLieu, lblLieu, lieu);
            }
            if (!string.IsNullOrEmpty(reference))
            {
                SetInformationSection(pnlRef, lblRef, reference);
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            Page.Form.Target = "_blank";
            if (!Page.IsPostBack)
            {
                if (Context.Request.QueryString["offre"] != null)
                {
                    string offre = Context.Request.QueryString["offre"].ToString();
                    using (SPSite site = new SPSite(SPContext.Current.Site.Url))
                    {
                        using (SPWeb web = site.OpenWeb())
                        {
                            SPList offreInterneList = web.GetList("/Lists/offres%20internes/");
                            if (offreInterneList != null)
                            {
                                SPFieldMultiLineText commentaireTxtField = offreInterneList.Fields[OffreInterneFields.CommentaireField] as SPFieldMultiLineText;
                                SPFieldText directionTxtField = offreInterneList.Fields[OffreInterneFields.DirectionField] as SPFieldText;
                                SPFieldText departementTxtField = offreInterneList.Fields[OffreInterneFields.DepartementField] as SPFieldText;
                                SPFieldText serviceTxtField = offreInterneList.Fields[OffreInterneFields.ServiceField] as SPFieldText;
                                SPFieldText lieuTravailTxtField = offreInterneList.Fields[OffreInterneFields.LieuField] as SPFieldText;
                                SPFieldText envTravailTxtField = offreInterneList.Fields[OffreInterneFields.EnvTravailField] as SPFieldText;
                                SPFieldText metierTxtField = offreInterneList.Fields[OffreInterneFields.MetierField] as SPFieldText;
                                SPFieldText intitulePosteTxtField = offreInterneList.Fields[OffreInterneFields.IntituleInternePosteField] as SPFieldText;
                                SPFieldText tempsTravailTxtField = offreInterneList.Fields[OffreInterneFields.TempsTravailField] as SPFieldText;
                                SPFieldDateTime dateCreaTxtField = offreInterneList.Fields[OffreInterneFields.DateField] as SPFieldDateTime;
                                SPFieldText urlTxtField = offreInterneList.Fields[OffreInterneFields.UrlField] as SPFieldText;
                                SPFieldMultiLineText descMissionTxtField = offreInterneList.Fields[OffreInterneFields.DescMissionField] as SPFieldMultiLineText;
                                SPFieldMultiLineText profilTxtField = offreInterneList.Fields[OffreInterneFields.ProfilField] as SPFieldMultiLineText;
                                SPFieldChoice activeChoiceField = offreInterneList.Fields[OffreInterneFields.ActiveField] as SPFieldChoice;
                                SPFieldText refTxtField = offreInterneList.Fields[OffreInterneFields.RefField] as SPFieldText;
                                StringBuilder queryBuilder = new StringBuilder();
                                SPListItem selectedOffre = offreInterneList.GetItemById(Int32.Parse(offre));

                                btnPostuler.PostBackUrl = urlTxtField.GetFieldValueAsText(selectedOffre[urlTxtField.InternalName]);

                                SetInformationsForm(commentaireTxtField.GetFieldValueAsHtml(selectedOffre[commentaireTxtField.InternalName]),
                                    directionTxtField.GetFieldValueAsText(selectedOffre[directionTxtField.InternalName]),
                                    departementTxtField.GetFieldValueAsText(selectedOffre[departementTxtField.InternalName]),
                                    serviceTxtField.GetFieldValueAsText(selectedOffre[serviceTxtField.InternalName]),
                                    envTravailTxtField.GetFieldValueAsText(selectedOffre[envTravailTxtField.InternalName]),
                                    metierTxtField.GetFieldValueAsText(selectedOffre[metierTxtField.InternalName]),
                                    intitulePosteTxtField.GetFieldValueAsText(selectedOffre[intitulePosteTxtField.InternalName]),
                                    tempsTravailTxtField.GetFieldValueAsText(selectedOffre[tempsTravailTxtField.InternalName]),
                                    descMissionTxtField.GetFieldValueAsHtml(selectedOffre[descMissionTxtField.InternalName]),
                                    profilTxtField.GetFieldValueAsHtml(selectedOffre[profilTxtField.InternalName]),
                                    lieuTravailTxtField.GetFieldValueAsText(selectedOffre[lieuTravailTxtField.InternalName]),
                                    refTxtField.GetFieldValueAsText(selectedOffre[refTxtField.InternalName]));
                            }
                        }
                    }
                }
            }
        }
    }
}
